#!/bin/bash
# todo: replace with command line parameter
build_base=`cd ..; pwd`
buildroot_base=${build_base}
project_base=output/build
kernel_base=linux-2.6.32
uclibc_base=output/toolchain/uClibc-0.9.30.2
busybox_base=busybox-1.16.0
filesystem_base=output/target

# start by letting the user select one of the available projects
dynconfig=.psconfig
# rm -f .config
touch ${dynconfig}
echo "# Dynamically generated Kconfig file for project selection" > ${dynconfig}
echo "menu \"Configure paths\"" >> ${dynconfig}
echo "config BASEDIR_BUILDROOT" >> ${dynconfig}
echo "string \"BuildRoot base directory\"" >> ${dynconfig}
echo "default \"${buildroot_base}\"" >> ${dynconfig}
echo "comment \"Paths relative to buildroot base directory\"" >> ${dynconfig}
echo "config BASEDIR_PROJECT" >> ${dynconfig}
echo "string \"Project base directory\"" >> ${dynconfig}
echo "default \"${project_base}\"" >> ${dynconfig}
echo "config BASEDIR_UCLIBC" >> ${dynconfig}
echo "string \"uClibc directory\"" >> ${dynconfig}
echo "default \"${uclibc_base}\"" >> ${dynconfig}
echo "comment \"Paths relative to project base directory\"" >> ${dynconfig}
echo "config BASEDIR_KERNEL" >> ${dynconfig}
echo "string \"Kernel directory\"" >> ${dynconfig}
echo "default \"${kernel_base}\"" >> ${dynconfig}
echo "config BASEDIR_BUSYBOX" >> ${dynconfig}
echo "string \"Busybox directory\"" >> ${dynconfig}
echo "default \"${busybox_base}\"" >> ${dynconfig}
echo "config BASEDIR_FILESYSTEM" >> ${dynconfig}
echo "string \"File system directory\"" >> ${dynconfig}
echo "default \"${filesystem_base}\"" >> ${dynconfig}
echo "endmenu" >> ${dynconfig}
echo "choice" >> ${dynconfig}
echo "    prompt \"Select operation\"" >> ${dynconfig}
echo "config OPERATION_SELECT" >> ${dynconfig}
echo "bool \"Load configuration\"" >> ${dynconfig}
echo "config OPERATION_SAVE_CURRENT_AS_NEW" >> ${dynconfig}
echo "bool \"Save current configuration as new\"" >> ${dynconfig}
echo "config OPERATION_UPDATE_FROM_CURRENT" >> ${dynconfig}
echo "bool \"Update existing configuration\"" >> ${dynconfig}
echo "config OPERATION_RENAME" >> ${dynconfig}
echo "bool \"Rename existing configuration\"" >> ${dynconfig}
echo "config OPERATION_DELETE" >> ${dynconfig}
echo "bool \"Delete existing configuration\"" >> ${dynconfig}
echo "config OPERATION_COMPARE" >> ${dynconfig}
echo "bool \"Compare two configurations\"" >> ${dynconfig}
echo "config OPERATION_VERIFY" >> ${dynconfig}
echo "bool \"Verify parameters\"" >> ${dynconfig}
echo "endchoice" >> ${dynconfig}
echo "config NEW_PROJECT_NAME" >> ${dynconfig}
echo "string \"New configuration name\"" >> ${dynconfig}
echo "depends on OPERATION_SAVE_CURRENT_AS_NEW || OPERATION_RENAME" >> ${dynconfig}
echo "choice" >> ${dynconfig}
echo "    prompt \"Select configuration\"" >> ${dynconfig}
echo "depends on OPERATION_SELECT || OPERATION_COMPARE || OPERATION_UPDATE_FROM_CURRENT || OPERATION_RENAME || OPERATION_DELETE" >> ${dynconfig}
find . -name Kconfig | sort | grep -e "\./.\+/Kconfig" | xargs cat >> ${dynconfig}
echo "endchoice" >> ${dynconfig}
echo "choice" >> ${dynconfig}
echo "    prompt \"Select configuration to compare with\"" >> ${dynconfig}
echo "depends on OPERATION_COMPARE" >> ${dynconfig}
find . -name Kconfig | sort | grep -e "\./.\+/Kconfig" | xargs cat | sed -e "s/PROJECT_/PROJECT_CMP_/" >> ${dynconfig}
echo "endchoice" >> ${dynconfig}
echo "config INCLUDE_FILE_SYSTEM" >> ${dynconfig}
echo "bool \"Save/restore target root file system\"" >> ${dynconfig}
echo "depends on OPERATION_SAVE_CURRENT_AS_NEW || OPERATION_UPDATE_FROM_CURRENT || OPERATION_SELECT" >> ${dynconfig}
echo "config INCLUDE_FILE_SYSTEM_SCRIPTS" >> ${dynconfig}
echo "bool \"Scripts only\"" >> ${dynconfig}
echo "depends on INCLUDE_FILE_SYSTEM" >> ${dynconfig}
KCONFIG_AUTOCONFIG=auto.conf KCONFIG_AUTOHEADER=autoconf.h ${build_base}/package/config/mconf ${dynconfig}
read -sn 1 -p "Proceed with the operations [y/n]?" choice
echo
if [ "$choice" = "y" ]
then
perl selproj.pl ${buildroot_base} .config
else
echo "Operation cancelled"
fi
